package com.show.sms;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.tea.TeaException;
import com.show.business.common.constant.HttpStatus;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.integrate.bean.SmsConfig;

import java.util.Date;

/**
 * 短信发送功能
 * @author ccx
 */
public class AliSmsUtil {
    /**
     * 使用AK&SK初始化账号Client
     * @param smsConfig
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(SmsConfig smsConfig) throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(smsConfig.getAccessKeyId())
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(smsConfig.getAccessKeySecret());
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信
     * @param smsConfig
     * @return
     * @throws Exception
     */
    public static AjaxResult sendSms(SmsConfig smsConfig){
        if(StrUtil.hasBlank(smsConfig.getSignName(),smsConfig.getAccessKeyId(),smsConfig.getAccessKeySecret()
        ,smsConfig.getPhoneNumbers(),smsConfig.getAddress(),smsConfig.getCode(),smsConfig.getLine(),smsConfig.getTemplateCode())){
            return AjaxResult.error("短信发送参数有误");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("address",smsConfig.getAddress());
        jsonObject.putOnce("code",smsConfig.getCode());
        jsonObject.putOnce("time",smsConfig.getTime());
        jsonObject.putOnce("line",smsConfig.getLine());
        AjaxResult result = new AjaxResult();
        try {
            com.aliyun.dysmsapi20170525.Client client = AliSmsUtil.createClient(smsConfig);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(smsConfig.getPhoneNumbers())
                    .setTemplateCode(smsConfig.getTemplateCode())
                    .setTemplateParam(jsonObject.toString())
                    .setSignName(smsConfig.getSignName());
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
            result.put(AjaxResult.CODE_TAG, HttpStatus.SUCCESS);
            result.put(AjaxResult.MSG_TAG,"发送成功");
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"发送失败");
            result.put(AjaxResult.DATA_TAG,error.getMessage());

        } catch (Exception _error) {
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"发送失败");
            result.put(AjaxResult.DATA_TAG,_error.getMessage());
        }
        return result;
    }

    /**
     * 发送通信异常短信
     * @param smsConfig
     * @return
     * @throws Exception
     */
    public static AjaxResult sendOnlineSms(SmsConfig smsConfig){
        if(StrUtil.hasBlank(smsConfig.getSignName(),smsConfig.getAccessKeyId(),smsConfig.getAccessKeySecret()
                ,smsConfig.getPhoneNumbers(),smsConfig.getAddress(),smsConfig.getCode(),smsConfig.getTemplateCode())){
            return AjaxResult.error("短信发送参数有误");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("address",smsConfig.getAddress());
        jsonObject.putOnce("code",smsConfig.getCode());
        jsonObject.putOnce("time",smsConfig.getTime());
        AjaxResult result = new AjaxResult();
        try {
            com.aliyun.dysmsapi20170525.Client client = AliSmsUtil.createClient(smsConfig);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(smsConfig.getPhoneNumbers())
                    .setTemplateCode(smsConfig.getTemplateCode())
                    .setTemplateParam(jsonObject.toString())
                    .setSignName(smsConfig.getSignName());
            // 复制代码运行请自行打印 API 的返回值
            client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());
            result.put(AjaxResult.CODE_TAG, HttpStatus.SUCCESS);
            result.put(AjaxResult.MSG_TAG,"发送成功");
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"发送失败");
            result.put(AjaxResult.DATA_TAG,error.getMessage());

        } catch (Exception _error) {
            result.put(AjaxResult.CODE_TAG, HttpStatus.ERROR);
            result.put(AjaxResult.MSG_TAG,"发送失败");
            result.put(AjaxResult.DATA_TAG,_error.getMessage());
        }
        return result;
    }

    public static void main(String[] args) {
        SmsConfig smsConfig = new SmsConfig();
        smsConfig.setAccessKeyId("LTAI5tPHSUe7MMWZdXZvULrQ");
        smsConfig.setAccessKeySecret("KuUahoJYbCWATjnOcb8BUMpvhSExJU");
        smsConfig.setSignName("顺悦鑫");
        smsConfig.setTemplateCode("SMS_474520254");
        smsConfig.setAddress("函谷关隧道变电房");
        smsConfig.setCode("DL20240002");
        smsConfig.setTime("2024-10-16 12:20:30");
        smsConfig.setLine("测试告警线路");
        smsConfig.setPhoneNumbers("13140165732");
        System.out.println(JSONUtil.toJsonStr(sendSms(smsConfig)));
    }
}
